package com.ymatou.promotion.api.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.parameter.UpdateSellerCouponBatchOpenBean;
import com.ymatou.promotion.api.service.UpdateSellerCouponBatchOpenCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponSellerPrefix;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponSellerPrefixWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 开启无门槛领取优惠券
 * FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UpdateSellerCouponBatchOpen {
    private static UpdateSellerCouponBatchOpenBean updatesellercouponbatchopenBean;
    private static UpdateSellerCouponBatchOpenCall updatesellercouponbatchopenCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("开启无门槛领取优惠券");
    }

    @Before
    public void caseUp() {
        updatesellercouponbatchopenBean = new UpdateSellerCouponBatchOpenBean();
        updatesellercouponbatchopenCall = new UpdateSellerCouponBatchOpenCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(updatesellercouponbatchopenCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_001() {
        Logger.start(true, "开启状态调用开启无门槛领取优惠券接口会关闭无门槛领取功能");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            new YmtCouponSellerPrefixWapper().updateAllIsOpenForiOperatorid(sellerId);

            CouponBatchBean couponBatchBean = new CouponCallService().setDefaultCouponBatchBean();
            couponBatchBean.setSellerId(sellerId);

            String batchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();

            // 数据库中状态位设置成开启状态
            new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, true);

            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchcode).get(0);
            new YmtCouponBatchWapper().setIsShowInPage(batchcode, false);

            updatesellercouponbatchopenBean.setSellerId(sellerId);
            updatesellercouponbatchopenBean.setBatchCode(batchcode);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            YmtCouponSellerPrefix ymtCouponSellerPrefix =
                    new YmtCouponSellerPrefixWapper().selectBysBatchCode(batchcode).get(0);

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

            Logger.verifyEquals(false,
                    batch.getIsShowInPage(),
                    "验证数据库表Ymt_CouponBatch中IsShowInPage");

            Logger.verifyEquals(false,
                    ymtCouponSellerPrefix.getIsOpen(),
                    "验证数据库Ymt_CouponSellerPrefix中该记录的Isopen=0");

            // 数据库中状态位设置成关闭状态，防止影响后面的开启
            new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, false);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_002() {
        Logger.start(true, "关闭状态调用开启无门槛领取优惠券接口会开启无门槛领取功能");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            new YmtCouponSellerPrefixWapper().updateAllIsOpenForiOperatorid(sellerId);

            CouponBatchBean couponBatchBean = new CouponCallService().setDefaultCouponBatchBean();
            couponBatchBean.setSellerId(sellerId);

            String batchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();

            new YmtCouponBatchWapper().setIsShowInPage(batchcode, false);

            // 数据库中状态位设置成开启状态
            new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, false);

            updatesellercouponbatchopenBean.setSellerId(sellerId);
            updatesellercouponbatchopenBean.setBatchCode(batchcode);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            YmtCouponSellerPrefix ymtCouponSellerPrefix =
                    new YmtCouponSellerPrefixWapper().selectBysBatchCode(batchcode).get(0);

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchcode).get(0);

            Logger.verifyEquals(true,
                    batch.getIsShowInPage(),
                    "验证数据库表Ymt_CouponBatch中IsShowInPage");

            Logger.verifyEquals(true,
                    ymtCouponSellerPrefix.getIsOpen(),
                    "验证数据库Ymt_CouponSellerPrefix中该记录的Isopen=1");

            // 数据库中状态位设置成关闭状态，防止影响后面的开启
            new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, false);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_005() {
        Logger.start(true, "新创建的批次调用开启无门槛领取优惠券接口会开启无门槛领取功能");
        try {
            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            new YmtCouponSellerPrefixWapper().updateAllIsOpenForiOperatorid(sellerId);

            CouponBatchBean couponBatchBean = new CouponCallService().setDefaultCouponBatchBean();
            couponBatchBean.setSellerId(sellerId);

            String batchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();

            updatesellercouponbatchopenBean.setSellerId(sellerId);
            updatesellercouponbatchopenBean.setBatchCode(batchcode);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Thread.sleep(1000);
            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

            YmtCouponSellerPrefix ymtCouponSellerPrefix =
                    new YmtCouponSellerPrefixWapper().selectBysBatchCode(batchcode).get(0);

            Logger.verifyEquals(true,
                    ymtCouponSellerPrefix.getIsOpen(),
                    "验证数据库Ymt_CouponSellerPrefix中该记录的Isopen=1");

            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchcode).get(0);
            Logger.verifyEquals(true,
                    batch.getIsShowInPage(),
                    "验证数据库表Ymt_CouponBatch中IsShowInPage");

            // 数据库中状态位设置成关闭状态，防止影响后面的开启
            new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, false);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_003() {
        Logger.start(true, "单个商家有十个有效开启领取的批次后就不能再开启");
        try {
            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            new YmtCouponSellerPrefixWapper().updateAllIsOpenForiOperatorid(sellerId);

            CouponBatchBean couponBatchBean = new CouponCallService().setDefaultCouponBatchBean();
            couponBatchBean.setSellerId(sellerId);

            List<String> batchCodes = new ArrayList<String>();

            for (int i = 0; i < 10; i++)
            {
                String batchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();
                batchCodes.add(batchcode);
                new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, true);
            }

            String newbatchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();

            updatesellercouponbatchopenBean.setSellerId(sellerId);
            updatesellercouponbatchopenBean.setBatchCode(newbatchcode);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Thread.sleep(1000);
            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("false",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

            Logger.verifyEquals("最多支持对10个优惠券活动开启“无门槛领取”服务，您已超过上限。",
                    updatesellercouponbatchopenCall.getMsgInData(),
                    "验证返回Status");

            YmtCouponSellerPrefix ymtCouponSellerPrefix =
                    new YmtCouponSellerPrefixWapper().selectBysBatchCode(newbatchcode).get(0);

            Logger.verifyEquals(null,
                    ymtCouponSellerPrefix.getIsOpen(),
                    "验证数据库Ymt_CouponSellerPrefix中该记录的Isopen=0");

            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(newbatchcode).get(0);
            Logger.verifyEquals(false,
                    batch.getIsShowInPage(),
                    "验证数据库表Ymt_CouponBatch中IsShowInPage");

            // 数据库中状态位设置成关闭状态，防止影响后面的开启
            for (String batchCode : batchCodes) {
                new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode, false);
            }

            new YmtCouponSellerPrefixWapper().updateIsOpen(newbatchcode, false);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_004() {
        Logger.start(true, "单个商家有十个有效开启领取的批次中有一个过期后，能开启新的批次");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            new YmtCouponSellerPrefixWapper().updateAllIsOpenForiOperatorid(sellerId);

            CouponBatchBean couponBatchBean = new CouponCallService().setDefaultCouponBatchBean();
            couponBatchBean.setSellerId(sellerId);

            List<String> batchCodes = new ArrayList<String>();

            for (int i = 0; i < 10; i++)
            {
                String batchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();
                batchCodes.add(batchcode);
                new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, true);
            }

            // 关闭一个批次
            updatesellercouponbatchopenBean.setSellerId(sellerId);
            updatesellercouponbatchopenBean.setBatchCode(batchCodes.get(0));

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            //
            String newbatchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();

            updatesellercouponbatchopenBean.setSellerId(sellerId);
            updatesellercouponbatchopenBean.setBatchCode(newbatchcode);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

            YmtCouponSellerPrefix ymtCouponSellerPrefix =
                    new YmtCouponSellerPrefixWapper().selectBysBatchCode(newbatchcode).get(0);

            Logger.verifyEquals(true,
                    ymtCouponSellerPrefix.getIsOpen(),
                    "验证数据库Ymt_CouponSellerPrefix中该记录的Isopen=1");

            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(newbatchcode).get(0);
            Logger.verifyEquals(true,
                    batch.getIsShowInPage(),
                    "验证数据库表Ymt_CouponBatch中IsShowInPage");


            // 数据库中状态位设置成关闭状态，防止影响后面的开启
            for (String batchCode : batchCodes) {
                new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode, false);
            }

            new YmtCouponSellerPrefixWapper().updateIsOpen(newbatchcode, false);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_006() {
        Logger.start(true, "单个商家有十个有效开启领取的批次中有一个作废后，能开启新的批次");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            new YmtCouponSellerPrefixWapper().updateAllIsOpenForiOperatorid(sellerId);

            CouponBatchBean couponBatchBean = new CouponCallService().setDefaultCouponBatchBean();
            couponBatchBean.setSellerId(sellerId);

            List<String> batchCodes = new ArrayList<String>();

            for (int i = 0; i < 10; i++)
            {
                String batchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();
                batchCodes.add(batchcode);
                new YmtCouponSellerPrefixWapper().updateIsOpen(batchcode, true);
            }

            // 关闭一个批次
            CouponCallService.inValidCoupon(batchCodes.get(0));

            // 创建新批次
            String newbatchcode = new CouponCallService().createSellerCoupon(couponBatchBean).getBatchCode();

            updatesellercouponbatchopenBean.setSellerId(sellerId);
            updatesellercouponbatchopenBean.setBatchCode(newbatchcode);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

            YmtCouponSellerPrefix ymtCouponSellerPrefix =
                    new YmtCouponSellerPrefixWapper().selectBysBatchCode(newbatchcode).get(0);

            Logger.verifyEquals(true,
                    ymtCouponSellerPrefix.getIsOpen(),
                    "验证数据库Ymt_CouponSellerPrefix中该记录的Isopen=1");

            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(newbatchcode).get(0);
            Logger.verifyEquals(true,
                    batch.getIsShowInPage(),
                    "验证数据库表Ymt_CouponBatch中IsShowInPage");


            // 数据库中状态位设置成关闭状态，防止影响后面的开启
            for (String batchCode : batchCodes) {
                new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode, false);
            }

            new YmtCouponSellerPrefixWapper().updateIsOpen(newbatchcode, false);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_007() {
        Logger.start(true, "batchcode空");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            updatesellercouponbatchopenBean.setSellerId(sellerId);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("false",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_008() {
        Logger.start(true, "batchcode不存在");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            updatesellercouponbatchopenBean.setBatchCode("91605065806");
            updatesellercouponbatchopenBean.setSellerId(sellerId);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_009() {
        Logger.start(true, "SellerId空");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            String batchCode = new YmtCouponBatchWapper().getTop1CouponBatch().get(0).get("sBatchCode").toString();
            updatesellercouponbatchopenBean.setBatchCode(batchCode);
            // updatesellercouponbatchopenBean.setSellerId(sellerId);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("false",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_010() {
        Logger.start(true, "SellerId不存在-有问题，未做sellerId验证");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            String batchCode = new YmtCouponBatchWapper().getTop1CouponBatch().get(0).get("sBatchCode").toString();
            updatesellercouponbatchopenBean.setBatchCode(batchCode);
            updatesellercouponbatchopenBean.setSellerId(9999999);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_UpdateSellerCouponBatchOpen_011() {
        Logger.start(true, "BatchCode与SellerId不匹配-有问题，未做sellerId验证");
        try {

            int sellerId = Integer.parseInt(EnvSetup.getData("couponSellerId"));

            String batchCode = new CouponCallService().createSellerDefaultCoupon();

            updatesellercouponbatchopenBean.setBatchCode(batchCode);
            updatesellercouponbatchopenBean.setSellerId(sellerId);

            updatesellercouponbatchopenCall.setData(updatesellercouponbatchopenBean);
            updatesellercouponbatchopenCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    updatesellercouponbatchopenCall.getCode(),
                    "验证返回Code");

            Logger.verifyEquals(ECode.BCODE0,
                    updatesellercouponbatchopenCall.getBCode(),
                    "验证返回Code");

            Logger.verifyEquals("true",
                    updatesellercouponbatchopenCall.getStatus(),
                    "验证返回Status");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
